﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMTC.Dao.BU;
using SMTC.Model.Domain;
using System.Data.Common;
using SMTC.Model.Model;
using SMTC.BO.Common;

namespace SMTC.BO.BU
{
    public class BUProccessServices
    {
        private BUProccessDao _dao = new BUProccessDao();
        /// <summary>
        /// 插入BU
        /// </summary>
        /// <param name="domain"></param>
        /// <returns></returns>
        public bool InsertBU(BUDomain domain)
        {
            DbTransaction dbt = null;
            bool operResult = true;

            try
            {
                domain = this.ProccessCapacity(domain);
                operResult = operResult && _dao.InsertBu(domain.BU, ref dbt);
                foreach (var item in domain.LineList)
                {
                    operResult = operResult && _dao.InsertBusLine(item, ref dbt);
                }

                if (operResult == true)
                {
                    if (dbt != null) dbt.Commit();
                    return true;
                }
                else
                {
                    if (dbt != null) dbt.Rollback();
                    return false;
                }
            }
            catch
            {
                if (dbt != null) dbt.Rollback();
                return false;
            }
        }
        /// <summary>
        /// 删除BU
        /// </summary>
        /// <param name="LineID"></param>
        /// <returns></returns>
        public bool DeleteBU(string BUID)
        {
            DbTransaction dbt = null;
            bool operResult = true;

            try
            {
                operResult = operResult && _dao.DeleteBu(BUID, ref dbt);
                operResult = operResult && _dao.DeleteBusline(BUID, ref dbt);

                if (operResult == true)
                {
                    if (dbt != null) dbt.Commit();
                    return true;
                }
                else
                {
                    if (dbt != null) dbt.Rollback();
                    return false;
                }
            }
            catch
            {
                if (dbt != null) dbt.Rollback();
                return false;
            }
        }
        /// <summary>
        /// 更新BU信息
        /// </summary>
        /// <param name="domain"></param>
        /// <returns></returns>
        public bool UpdateBU(BUDomain domain)
        {
            DbTransaction dbt = null;
            bool operResult = true;

            try
            {
                domain = this.ProccessCapacity(domain);
                operResult = operResult && _dao.UpdateBU(domain.BU, ref dbt);
                operResult = operResult && _dao.DeleteBusline(domain.BU.BUID, ref dbt);
                foreach (var item in domain.LineList)
                {
                    operResult = operResult && _dao.InsertBusLine(item, ref dbt);
                }

                if (operResult == true)
                {
                    if (dbt != null) dbt.Commit();
                    return true;
                }
                else
                {
                    if (dbt != null) dbt.Rollback();
                    return false;
                }
            }
            catch
            {
                if (dbt != null) dbt.Rollback();
                return false;
            }
        }

        /// <summary>
        /// 计算BU能力
        /// </summary>
        /// <param name="domain"></param>
        /// <returns></returns>
        public BUDomain ProccessCapacity(BUDomain domain)
        {
            var result = from item in domain.LineList
                         group item by item.BUID into g
                         select new SMT_LineModel
                         {
                             LineCapacity = g.Sum(a => a.LineCapacity)
                         };
            List<SMT_LineModel> list = result.Cast<SMT_LineModel>().ToList<SMT_LineModel>();
            if (list != null && list.Count > 0)
            {
                domain.BU.BUCapacity = list[0].LineCapacity;
            }
            return domain;
        }
    }
}
